// import axios from 'axios'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentindex:0,
    option:[{name:"默认",status:0},{name:"新品",status:0},{name:"销量",status:0},{name:"价格",status:0}],
    default:[],
    xiao:0,
    user:[],
    ceshi:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that=this
    wx.getSetting({

      success(res){
        if (!res.authSetting['scope.record']) {
          wx.authorize({
              scope: 'scope.record',
              success() {
                  wx.getUserProfile({
                    desc: "完善用户资料",
                    success: (res) => {
                      console.log("授权成功");
                      that.data.user.push(res.userInfo)
                    },
                    fail: (res) => {
                      console.log("授权失败");
                    },
                  });
              }
          })
      }
      }
    })
    wx.request({
      url: 'http://172.20.10.2',
      dataType: 'json',
      success(res) {
        that.setData({default:res.data[0].data})
      }
    })
  },
  homeyi:function() {
    var that = this;
  let xiao= wx.getStorageSync('user')
    this.setData({ceshi:false})
   if(xiao.length<=0&&this.data.ceshi==false){
    wx.getUserProfile({
      desc: "完善用户资料",
      success: (res) => {
        that.data.user.push(res.userInfo)
        wx.setStorageSync('user',that.data.user)
        wx.showToast({
          title: '授权成功',
        })
      },
      fail: (res) => {
        console.log("授权失败");
      },
    });
   }
  },
  homePage:function(e){
    let xiao= wx.getStorageSync('user')
    console.log(xiao);
    if(xiao.length>0){
      let title=e.currentTarget.dataset.id
    this.data.option[3].status=title
    
    this.setData({currentindex:title})
    var that=this 
    wx.request({
      url: 'http://172.20.10.2',
      dataType: 'json',
      success(res) {
        if(that.data.currentindex===2){
         var  str= res.data[0].data
        function del(v){
          return function(a,b){
            return b[v] - a[v]
          }
        }
         var newArray=str.sort(del('Purchase'))
         that.data.default=newArray 
        }
        if(that.data.currentindex==0){
          that.setData({default:res.data[0].data})
        }else if(that.data.currentindex==2){
          that.setData({default:res.data[0].data})
        }else if(that.data.currentindex==3){
          that.setData({default:res.data[0].data})
        }else{
          that.setData({default:res.data[1].data})
        }
        setTimeout(()=>{
          that.setData({default:newArray})
        },50)
      }
    })
    this.data.option.forEach((res,i) => {
      if(i!=title){
        res.status=0
      }
  });
  if(title==this.data.currentindex){
    if(this.data.xiao!=1){
      this.data.option[title].status=this.data.option[title].status==3?1:''
      this.setData({option:this.data.option})
      this.setData({xiao:1})
      wx.request({
        url: 'http://172.20.10.2',
        dataType: 'json',
        success(res) {
          if(that.data.currentindex===3){
           var  str= res.data[0].data
          function del(v){
            return function(a,b){
              return a[v] - b[v]
            }
          }
           var newArray=str.sort(del('Price'))
           that.data.default=newArray
          }
          that.setData({default:newArray})
        }
      })
    }else{
      this.data.option[title].status=this.data.option[title].status==3?2:''
      this.setData({option:this.data.option})
      this.setData({xiao:0})
      wx.request({
        url: 'http://172.20.10.2',
        dataType: 'json',
        success(res) {
          if(that.data.currentindex===3){
           var  str= res.data[0].data
          function del(v){
            return function(a,b){
              return b[v] - a[v]
            }
          }
           var newArray=str.sort(del('Price'))
           that.data.default=newArray
          }
          that.setData({default:newArray})
        }
      })
    }
  }
    }
  },
  details:function(e){
    let xiao= wx.getStorageSync('user')
    console.log(xiao);
    if(xiao.length>0){
      let lise=e.currentTarget.dataset.details
        wx.navigateTo({
      url: '../detail-page/index?id='+JSON.stringify(lise.id),
    })
    }
  }
})